package basicservicelogic

import (
	"context"
	"property_service/dao/model"
	"property_service/dao/query"
	"property_service/internal/svc"
	"property_service/property_service"
	"property_service/utils/result"

	"github.com/zeromicro/go-zero/core/logx"
)

type DictionaryMainEditLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewDictionaryMainEditLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DictionaryMainEditLogic {
	return &DictionaryMainEditLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// DictionaryMainEdit 更新字典主表
func (l *DictionaryMainEditLogic) DictionaryMainEdit(in *property_service.DictionaryMain) (*property_service.Response, error) {
	// 获取字典主表模型的查询构造器
	m := query.Use(l.svcCtx.Gorm).DictionaryMain
	// 构建查询条件：根据ID查询
	do := m.WithContext(l.ctx).Where(m.ID.Eq(in.ID))
	// 执行更新操作，仅更新指定字段，不包含ID、TenantID、CreatedAt、UpdatedAt
	if _, err := do.UpdateColumns(&model.DictionaryMain{
		DictionaryName: in.DictionaryName, // 字典名称
		DictKey:        in.DictKey,        // 字段唯一key
		Code:           in.Code,           // 字典编码
		Description:    in.Description,    // 字典描述
		UpdatedAtuser:  in.UpdatedAtuser,  // 修改人ID
	}); err != nil {
		// 如果更新失败，返回错误响应
		return result.Err(err).Ed()
	}
	// 更新成功，返回成功响应
	return result.Suc().EditEd()
}
